import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mario
 */

public class servicioDao {

    private Connection conexion;

    public servicioDao() throws SQLException{
        this.conexion = conexionServicios.getConexion();
    }  
 
    public List<Servicio> getListaServiciosAll() throws SQLException{
        String sql = "SELECT * FROM servicios";
        PreparedStatement stmt = this.conexion.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        List<Servicio> listServicios = new ArrayList<Servicio>();

        while(rs.next()){
            Servicio nuevoServicio = new Servicio();
            nuevoServicio.setId_servicio(rs.getLong("id_servicio"));
            //System.out.println(nuevoServicio.getId_servicio());
            nuevoServicio.setName_servicio(rs.getString("name_servicio"));
            //System.out.println(nuevoServicio.getName_servicio());
            nuevoServicio.setCmd_servicio(rs.getString("cmd_servicio"));
            //System.out.println(nuevoServicio.getCmd_servicio());
            nuevoServicio.setDate_servicio(rs.getString("date_servicio"));
            //System.out.println(nuevoServicio.getDate_servicio());
            listServicios.add(nuevoServicio);
        }
        rs.close();
        stmt.close();
        return listServicios;
    }

    public void addServicio(Servicio newServicio) throws SQLException {
        String sql = "INSERT INTO servicios (name_servicio, cmd_servicio) values(?,?)";
        PreparedStatement stmt = this.conexion.prepareStatement(sql);
        stmt.setString(1, newServicio.getName_servicio());
        stmt.setString(2, newServicio.getCmd_servicio());
        stmt.execute();
        stmt.close();
    }

    public void deleteServicio(Servicio servicio) throws SQLException{
        String sql = "DELETE FROM servicios WHERE id_servicio = ?";
        PreparedStatement stmt = this.conexion.prepareStatement(sql);
        stmt.setLong(1,servicio.getId_servicio());
        stmt.execute();
        stmt.close();
    }

    public String selectServicio(int num) throws SQLException{
        String sql = "SELECT * FROM servicios WHERE id_servicio = ?";
        PreparedStatement stmt = this.conexion.prepareStatement(sql);
        stmt.setLong(1,num);        
        ResultSet rs = stmt.executeQuery();
        String str = new String();
        while(rs.next()){
        str = rs.getString("cmd_servicio");        
        }        
        rs.close();
        stmt.close();
        return str;
    }
/*
    public static void main(String[] args) {
        try {
            //Long a = new Long(1);
            servicioDao ser = new servicioDao();
            String str = ser.selectServicio(1);
            System.out.println(str);

        } catch (SQLException ex) {
            Logger.getLogger(servicioDao.class.getName()).log(Level.SEVERE, null, ex);
        }

    }*/
}
